import {reqlogin,reqHomepageList,reqSearchWindowList,reqRealtimeList,reqClearhistoryList,
  reqpRoductsearchList,reqBranddetailsList,reqRelatedproductsList,reqGetTopicList,reqAllclassificationList,
  reqCurrentclassificationList,reqProductquantityList,reqAddtocartList,reqShoppingcartList,reqCommoditystepperList,
  reqDeletecommodityList,reqSelectedstateList
} from '@/api/api.js'
import { Toast } from 'vant';
const state={
  //登录请求
  login:{},
  //首页数据
   Homepage:{},
  show:'',
  //热门搜索数据
  SearchWindowList:{},
  //实时搜索数据
  RealtimeList:{},
  //搜索框商品搜索
  RoductsearchList:{},
  //商品详情
  BranddetailsList:{},
  //相关产品
  RelatedproductsList:[],
  //专题要求
  TopicList:{},
  //全部分类数据请求
  ficationList:{},
  //获取当前分类列表
  Currentclassi:{},
  //获取当前购物车产品数量
  cartTotal:{},
  //加入购物车
  errno:'',
  //获取购物车数据
  ShoppingcartList:{},
  //商品步进器
  GoodsAmount:{},
  //删除商品
  commodityList:{},
  //切换商品选择状态
  SelectedstateList:{}
};

const mutations= {
  //登录请求
  GETLOGIN(state,login){
    state.login = login
    state.show = false
  },
  //获取首页数据请求
  GETHOMEPAGELIST(state,Homepage){
    state.Homepage = Homepage
  },
  //获取热门搜索
  GETSEARCHLIST(state,SearchWindowList){
    state.SearchWindowList = SearchWindowList
  },
  //实时搜索数据
  GETREALTIMELIST(state,RealtimeList){
    state.RealtimeList = RealtimeList
  },
  //搜索框商品搜索
  GETRODUCTSEARCHLIST(state,RoductsearchList){
    state.RoductsearchList = RoductsearchList
  },
  //商品详情
  GETBRANDDETAISLIST(state,BranddetailsList){
    state.BranddetailsList = BranddetailsList
  },
  //相关产品
  GETRELATEDPRODUTSLIST(state,RelatedproductsList){
      state.RelatedproductsList = RelatedproductsList
  },
  //专题要求
  GETGETTOPICLIST(state,TopicList){
    state.TopicList = TopicList
  },
  //全部分类数据请求
  GETALLCLASSLIST(state,ficationList){
      state.ficationList = ficationList
  },
  //获取当前分类列表
  GETCURRENTSLIST(state,Currentclassi){
    state.Currentclassi = Currentclassi
  },
  //获取当前购物车产品数量
  GETPRODUCTQULIST(state,cartTotal){
    state.cartTotal = cartTotal
  },
  //加入购物车
  GETADDCART(state,errno){{
    state.errno = errno
  }},
  //获取购物车数据
  GETSHOPPINGCARTLIST(state,ShoppingcartList){
    state.ShoppingcartList = ShoppingcartList
  },
  //商品步进器
  GETCOMMODITYLIST(state,GoodsAmount){
    state.GoodsAmount = GoodsAmount
  },
  //删除商品
  GETDELETELIST(state,commodityList){
    state.commodityList = commodityList
  },
  //切换商品选择状态
  GETSELECTEDSTATELIST(state,SelectedstateList){
    state.SelectedstateList = SelectedstateList
  }
};

const actions= {
  //登录请求
 async getlogin({commit},{username,pwd}){
  let result = await reqlogin({username,pwd})
  
  if(result.errno==0){
    commit('GETLOGIN',result)
    //将用户名和token保存到本地存储中
    localStorage.setItem('token',result.data.token)
    localStorage.setItem('errno',result.errno)
    localStorage.setItem('nickname',JSON.stringify(result.data.userInfo.nickname))
    Toast('登录成功')
  }if(result.errno==602){
    Toast('密码错误');
  }
  },
  //获取首页数据请求
  async getHomepageList({commit}){
   let result = await reqHomepageList()
   if(result.errno==0){
    commit('GETHOMEPAGELIST',result.data)
   }
  },
  //获取热门搜索数据
 async getSearchWindowList({commit}){
  let result = await reqSearchWindowList()
  if(result.errno==0){
    commit("GETSEARCHLIST",result.data)
  }
  },
  //实时搜索功能
 async getRealtimeList({commit},keyword){
  let result = await reqRealtimeList(keyword)
  if(result.errno==0){
    commit('GETREALTIMELIST',result.data)
  }
  },
  //删除历史记录
 async getClearhistoryList({commit}){
  let result = await reqClearhistoryList()
  if(result.errno==0){
    Toast('删除记录成功')
  }
  },
  //搜索框商品搜索
 async getpRoductsearchList({commit},{keyword,order}){
  let result = await reqpRoductsearchList({keyword,order})
  if(result.errno==0){
    commit('GETRODUCTSEARCHLIST',result.data)
  }
  
  },
  //品牌详情
 async getBranddetailsList({commit},id){
  let result = await reqBranddetailsList(id)
  if(result.errno==0){
    commit('GETBRANDDETAISLIST',result.data)
  }
  },
  //相关产品
 async getRelatedproductsList({commit},id){
  let result = await reqRelatedproductsList(id)
  console.log(result);
  if(result.errno==0){
    commit('GETRELATEDPRODUTSLIST',result.data.goodsList)
  }
  },
  //专题请求
 async getGetTopicList({commit},{page,size}){
  let result = await reqGetTopicList({page,size})
  if(result.errno==0){
    commit('GETGETTOPICLIST',result.data)
  }
  },
  //全部分类数据请求
 async getAllclassificationList({commit}){
  let result = await reqAllclassificationList()
  if(result.errno==0){
    commit('GETALLCLASSLIST',result.data)
  }
  },
  //获取当前分类列表
 async getCurrentclassificationList({commit},id){
  let result = await reqCurrentclassificationList(id)
  if(result.errno==0){
    commit('GETCURRENTSLIST',result.data.currentCategory)
  }
  },
  //获取当前购物车产品数量
 async getProductquantityList({commit}){
  let result = await reqProductquantityList()
  if(result.errno==0){
    commit('GETPRODUCTQULIST',result.data.cartTotal)
  }
  },
  //加入购物车
 async getAddtocartList({commit},{goodsId,productId,number}){
  let result = await reqAddtocartList({goodsId,productId,number})
  if(result.errno==0){
    console.log(result);
    commit('GETADDCART',result)
  }
  },
  //获取购物车数据
 async getShoppingcartList({commit}){
  let result = await reqShoppingcartList()
  console.log(result);
  if(result.errno==0){
    commit('GETSHOPPINGCARTLIST',result.data)
  }
  },
  //商品步进器
 async getCommoditystepperList({commit},{goodsId,id,number,productId}){
  let result = await reqCommoditystepperList({goodsId,id,number,productId})
  console.log(result);
  if(result.errno==0){
    commit('GETCOMMODITYLIST',result.data.cartTotal)
  }
  },
  //删除商品
 async getDeletecommodityList({commit},productIds){
  let result = await reqDeletecommodityList(productIds.productIds.toString())
  console.log(result);
  if(result.errno==0){
    commit('GETDELETELIST',result.data)
  }
  },
  //切换商品选择状态
 async getSelectedstateList({commit},{isChecked,productIds}){
  let result = await reqSelectedstateList({isChecked,productIds})
  console.log(result);
  if(result.errno==0){
      commit('GETSELECTEDSTATELIST',result.data)
  }

  }
};

const getters= {
};

export default {
  state,
  mutations,
  actions,
  getters
}